<?php
/*
Plugin Name: WP-prettyPhoto
Plugin URI: http://blog.fusi0n.org/coding/wp-prettyphoto-lightbox-clone-for-wordpress-using-jquery
Description: WP-prettyPhoto allows you to use <a href="http://www.no-margin-for-errors.com/projects/prettyPhoto/" alt="prettyPhoto">prettyPhoto</a> by <a href="http://www.no-margin-for-errors.com/">Stephane Caron</a>, a jQuery-powered Lightbox clone with WordPress.
Version: 1.0.1
Author: Pier-Luc Petitclerc
Author URI: http://blog.fusi0n.org
*/

// Default values
add_option('wppp_automate', 1);
add_option('wppp_rel', 'prettyPhoto');
add_option('wppp_speed', 'normal');
add_option('wppp_padding', 40);
add_option('wppp_opacity', 0.35);
add_option('wppp_title', 'true');
add_option('wppp_resize', 'true');
add_option('wppp_counterlabel', '/');

if (!defined('WP_CONTENT_URL')) define( 'WP_CONTENT_URL', get_option('wpurl') . '/wp-content');
if (!defined('WP_CONTENT_DIR')) define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );

$wppp_path    = WP_CONTENT_DIR.'/plugins/'.plugin_basename(dirname(__FILE__).'/');
$wppp_options = WP_CONTENT_URL.'/plugins'.plugin_basename(dirname(__FILE__)).'/options-wpp.php';
$wppp_url     = WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__));

function wppp_admin_hook($links,$file) {
  static $this_plugin;
  if (!$this_plugin) { $this_plugin = plugin_basename(__FILE__); }
  if ($file == $this_plugin) {
    $settings_link = '<a href="options-general.php?page=wp-prettyphoto/options-wppp.php">' . __('Settings') . '</a>';
    array_unshift( $links, $settings_link ); // before other links
  }
  return $links;
}

function wppp_styles() {
  global $wppp_url;
  $wppp_css           = "$wppp_url/css/prettyPhoto.css";
  $wppp_rel           = get_option('wppp_rel');
  $wppp_speed         = get_option('wppp_speed');
  $wppp_padding       = get_option('wppp_padding');
  $wppp_opacity       = get_option('wppp_opacity');
  $wppp_title         = get_option('wppp_title') == 'true'? 'true' : 'false';
  $wppp_resize        = get_option('wppp_resize') == 'true'? 'true' : 'false';
  $wppp_counterlabel  = get_option('wppp_counterlabel');

  echo <<<EOHTML
    <link rel="stylesheet" href="{$wppp_css}" type="text/css" media="screen" />
    <script type="text/javascript" charset="utf-8">
      $(document).ready(function(){
        $("a[rel^='{$wppp_rel}']").prettyPhoto({
          animationSpeed: '{$wppp_speed}', /* fast/slow/normal */
          padding: {$wppp_padding}, /* padding for each side of the picture */
          opacity: {$wppp_opacity}, /* Value betwee 0 and 1 */
          showTitle: {$wppp_title}, /* true/false */
          allowresize: {$wppp_resize}, /* true/false */
          counter_separator_label: '{$wppp_counterlabel}' /* The separator for the gallery counter 1 "of" 2 */
        });
      });
    </script>
EOHTML;
}

/*
  From original author, Rupert Morris:
  Tanin, you are a regular expressions god! ;) 
  http://0xtc.com/2008/05/27/auto-lightbox-function.xhtml
*/
function wppp_content_hook($content) {
  global $post;
  $wppp_rel       = get_option('wppp_rel');
  $pattern[0]     = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\")([^\>]*?)>/i";
  $pattern[1]     = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\")(.*?)(rel=('|\")$wppp_rel(.*?)('|\"))([ \t\r\n\v\f]*?)((rel=('|\")$wppp_rel(.*?)('|\"))?)([ \t\r\n\v\f]?)([^\>]*?)>/i";
  $replacement[0] = '<a$1href=$2$3$4$5$6 rel="'.$wppp_rel.'[g'.$post->ID.']">';
  $replacement[1] = '<a$1href=$2$3$4$5$6$7>';
  $content        = preg_replace($pattern, $replacement, $content);
  return $content;
}

function wppp_add_options_page() {
  global $wppp_path;
  add_options_page('WP-prettyPhoto Options', 'WP-prettyPhoto', 'manage_options', $wppp_path.'options-wppp.php');
  add_filter( 'plugin_action_links', 'wppp_admin_hook', 10, 2 );
}

if (get_option('wppp_automate') == 1){
  add_filter('the_content', 'wppp_content_hook', 99);
  add_filter('the_excerpt', 'wppp_content_hook', 99);
}

if (!is_admin()) { wp_enqueue_script('prettyphoto', ("$wppp_url/js/jquery.prettyPhoto.js"), array('jquery'), '2.2.2'); }

add_action('wp_head', 'wppp_styles');
add_action('admin_head', 'wppp_add_options_page');
?>
